const {
  deletOne3,
  addOne3,
  getAll3,
  getOne3,
  updateOne3,
  search3,
  getTypes3,
  updateMany3
} = require("../dao/goodsDao");
const { typessearch } = require("../dao/typesDao");

const fs =require("fs")

// 第二层

// 渲染
module.exports.getAll2 = async ({
  nowPage,
  pageSize,
  searchType,
  searchValue,
}) => {
  if (searchValue != "") {
    console.log(searchType);
    console.log(searchValue);
    if (searchType == "typesname") {
      const data = await typessearch({
        nowPage,
        pageSize,
        searchType,
        searchValue,
      });

      return data;
    } else {
      return await search3({ nowPage, pageSize, searchType, searchValue });
    }
  } else {
    return await getAll3({ nowPage, pageSize });
  }
};
//删除
module.exports.deletOne2 = async function (reqdata) {
  const { listImage, detailImage } = reqdata;
  const data = await deletOne3(reqdata);

  if (data.deletedCount == 1) {
    // console.log();
  console.log(1818,listImage,detailImage,reqdata);
    
    if (listImage.length > 0) {
      for (let i in listImage) {
    
          fs.unlinkSync("public/files/" + listImage[i], (err) => {
            if (err) console.log(err);
          });
        
      }
    }
    if (detailImage.length > 0) {
      for (let i in detailImage) {
   
          fs.unlinkSync("public/files/" + detailImage[i], (err) => {
            if (err) console.log(err);
          });
        
      }
    }
    return true;
  } else {
    return false;
  }
};
//增加
module.exports.addOne2 = async (goods) => {
  const data = await addOne3(goods);
  return data;
};
//修改  1.获取
module.exports.getOne2 = async (_Id) => {
  return await getOne3(_Id);
};
//修改 2.确认修改
module.exports.updateOne2 = async (reqdata) => {
  const result = await updateOne3(reqdata);
  const { listImage, detailImage } = reqdata;
  if (result.ok == 1) {


    return true;
  } else {
    return false;
  }
};
module.exports.updateMany2 = async (reqdata) => {
  console.log(2222,reqdata);
  
  const result = await updateMany3(reqdata);
  // const { listImage, detailImage } = reqdata;
  if (result.ok == 1) {


    return true;
  } else {
    return false;
  }
};



module.exports.getTypes2 = async (_Id) => {
  return await getTypes3(_Id);
};
